package com.example.ssd4.domain;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Student {
    String id;
    String name;
    String group;
    String score;

    public Student(String id, String name, String group, String score) {
        this.id = id;
        this.name = name;
        this.group = group;
        this.score = score;
    }

    public static List<Student> readFromLogFile(String filePath) {
        List<Student> students = new ArrayList<>();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] parts = line.trim().replace("Student(", "").replace(")", "").split("\", \"");
                if (parts.length == 4) {
                    students.add(new Student(
                            parts[0].replace("\"", ""),
                            parts[1].replace("\"", ""),
                            parts[2].replace("\"", ""),
                            parts[3].replace("\"", "").trim()));
                }
            }
        } catch (FileNotFoundException ex){
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return students;
    }

    public static void updateScoreByStudentId(String filePath, String studentId, String newScore) {
        try (RandomAccessFile file = new RandomAccessFile(filePath, "rw")) {
            String line;
            long currentPosition = 0;
            while ((line = file.readLine()) != null) {
                String[] parts = line.trim().replace("Student(", "").replace(")", "").split("\", \"");
                if (parts.length == 4) {
                    String id = parts[0].replace("\"", "");
                    if (id.equals(studentId)) {
                        while (newScore.length() < 3){
                            newScore += " ";
                        }
                        // 找到匹配的学生，更新分数
                        String updatedLine = "Student(\"" + id + "\", \"" + parts[1] + "\", \"" + parts[2] + "\", \"" + newScore + "\")";
                        file.seek(currentPosition); // 定位到当前行的开头
                        file.writeBytes(updatedLine);
                        break;
                    }
                }
                currentPosition = file.getFilePointer(); // 记录当前位置
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getScore() {
        return score.trim();
    }

    public void setScore(String score) {
        this.score = score;
        while (score.length() < 3){
            score += " ";
        }
    }
}
